package com.zg.scf.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zg.common.core.dao.scf.AccAccountDao;
import com.zg.common.core.dao.scf.entity.AccAccount;
import com.zg.common.core.req.PageReq;
import com.zg.common.core.service.PageBaseService;
import com.zg.common.core.service.support.QueryWrapperBuilder;
import com.zg.common.core.util.DateUtil;
import com.zg.scf.domain.req.account.AccAccountEditSortReq;
import com.zg.scf.domain.req.account.AccAccountEditStateReq;
import com.zg.scf.domain.req.account.AccAccountPageReq;
import com.zg.scf.service.AccAccountService;
import com.zg.scf.constant.ScfE;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>Description: </p>
 *
 * @author zhanfeng.liang
 * @since 2025/5/22 10:47
 */

@Slf4j
@Service
public class AccAccountServiceImpl extends PageBaseService<AccAccount,AccAccountDao> implements AccAccountService {




    protected Page<AccAccount> pageInfo(Page<AccAccount> page, PageReq req) {
        AccAccountPageReq accountPageReq = (AccAccountPageReq) req; // 需要显式转换
        LambdaQueryWrapper<AccAccount> qw = new LambdaQueryWrapper<>();
        if (accountPageReq != null) {
            qw.eq(!StrUtil.isEmpty(accountPageReq.getAccountType()), AccAccount::getAccountType, accountPageReq.getAccountType());
            qw.eq(accountPageReq.getBankId() != null, AccAccount::getBankId, accountPageReq.getBankId());
            qw.eq(!StrUtil.isEmpty(accountPageReq.getBankAccount()), AccAccount::getBankAccount, accountPageReq.getBankAccount());
            qw.eq(accountPageReq.getSort() != null, AccAccount::getSort, accountPageReq.getSort());
        }
        accountPageReq.setEndTime(DateUtil.getToEndOfDay(accountPageReq.getEndTime()));
        Page<AccAccount> pageInfo = baseDao.pageInfo(page, qw, accountPageReq);
        return pageInfo;

    }

    public void syncBalance(Long accountId) {
        //TODO
        AccAccount accAccount = getById(accountId);
        baseDao.updateById(accAccount);
    }

    @Override
    protected void preEdit(AccAccount source) {
        AccAccount accAccount = getByBankAccount(source);
        if (accAccount != null && !accAccount.getId().equals(source.getId())) {
            ScfE.BANK_ACCOUNT_EXIST.happen();
        }
        accAccount = getAccAccounts(source);
        if (accAccount != null && !accAccount.getId().equals(source.getId())) {
            ScfE.BANK_ACCOUNT_EXIST.happen();
        }
    }


    @Override
    protected void preAdd(AccAccount source) {
        AccAccount accAccount = getAccAccounts(source);
        if (accAccount != null) {
            ScfE.ACCOUNT_BANK_TYPE_SORT_EXIST.happen();
        }
        accAccount = getByBankAccount(source);
        if (accAccount != null) {
            ScfE.BANK_ACCOUNT_EXIST.happen();
        }
    }

    private AccAccount getAccAccounts(AccAccount source) {
        AccAccount account = new AccAccount();
        account.setAccountType(source.getAccountType());
        account.setBankId(source.getBankId());
        account.setSort(0);
        List<AccAccount> accAccountList = list(account);
        if (accAccountList.isEmpty()) {
            return null;
        }
        return accAccountList.get(0);
    }

    private AccAccount getByBankAccount(AccAccount source) {
        AccAccount account = new AccAccount();
        account.setBankAccount(source.getBankAccount());
        List<AccAccount> accAccountList = list(account);
        if (accAccountList.isEmpty()) {
            return null;
        }
        return accAccountList.get(0);
    }

    @Override
    protected void preRemove(List<Long> idList) {
        List<AccAccount> list = baseDao.listByIds(idList);
        for(int i=0;i<list.size();i++){
            //TODO
        }
    }

   public void editState(AccAccountEditStateReq editReq) {
       AccAccount accAccount = new AccAccount();
       accAccount.setId(editReq.getId());
       accAccount.setState(editReq.getState());
       baseDao.updateById(accAccount);
   }

    public void editSort(AccAccountEditSortReq editReq) {
        AccAccount source = getById(editReq.getId());
        AccAccount accAccount = getAccAccounts(source);
        if (accAccount != null) {
            ScfE.ACCOUNT_BANK_TYPE_SORT_EXIST.happen();
        }
        accAccount = new AccAccount();
        accAccount.setId(editReq.getId());
        accAccount.setSort(editReq.getSort());
        baseDao.updateById(accAccount);
    }

    public static  void  main(String a[]) {
        //SFunction<AccAccount, String> func = MyLambdaUtils.create(AccAccount.class, "accountType");
        AccAccount accAccount =new AccAccount();
        accAccount.setAccountType("1");
        LambdaQueryWrapper builder = QueryWrapperBuilder.build(accAccount, AccAccount.class);
        System.out.println(builder);
        System.out.println(builder.getSqlSegment());
    }
}
